<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['applet:appletImg:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['applet:appletImg:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="appletImgList" border>
      <div slot="empty">
        <el-empty description="暂无任何图片素材！"></el-empty>
      </div>
      <el-table-column label="主键" align="center" prop="imgId" v-if="false"/>
      <el-table-column label="图片素材" align="center" prop="imgUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.imgUrl" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="OSS阿里云地址" align="center" prop="imgUrl" width="400" show-overflow-tooltip>

        <template slot-scope="scope">
          <u><a style="color: #00afff;display: inline-block;border-bottom: 1px solid #00afff"
                :href="scope.row.imgUrl"> {{ scope.row.imgUrl }}</a></u>
        </template>
      </el-table-column>
      <el-table-column label="请求接口地址" show-overflow-tooltip align="center" prop="imgUrl" width="180">
        <template slot-scope="scope">
          /without/img/getImg/{{ scope.row.imgId }}
        </template>
      </el-table-column>
      <el-table-column label="备注说明" align="center" show-overflow-tooltip prop="remark"/>
      <el-table-column label="创建时间" width="180" align="center" prop="createTime"/>
      <el-table-column label="创建账号" width="120" show-overflow-tooltip align="center" prop="createBy"/>
      <el-table-column label="更新时间" width="180" align="center" prop="updateTime"/>
      <el-table-column label="更新账号" width="120" show-overflow-tooltip align="center" prop="updateBy"/>
      <el-table-column label="操作" width="200" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['applet:appletImg:edit']"
          >修改
          </el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['applet:appletImg:remove']"-->
<!--          >删除-->
<!--          </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改appletImg对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <div v-if="isUpdate">
              <el-card shadow="hover">
                <el-row :gutter="10">
                  <el-col :span="12">
                    <el-image :src="form.imgUrl"></el-image>
                  </el-col>
                  <el-col :span="12">
                    <el-row>
                      <span style="color: #1ab394"><i class="el-icon-s-help"></i> 接口地址：</span>
                      /without/img/getImg/{{ form.imgId }}
                    </el-row>
                    <el-row style="margin-top: 10px">
                      <span style="color: #1ab394"><i class="el-icon-upload"></i> OSS阿里云地址：</span>
                      <u><a style="color: #00afff;display: inline-block;border-bottom: 1px solid #00afff"
                            :href="form.imgUrl"> {{ form.imgUrl }}</a></u>
                    </el-row>
                    <el-row style="margin-top: 10px">
                      <span style="color: #1ab394"><i class="el-icon-warning"></i> 素材说明：</span>
                      {{ form.remark }}
                    </el-row>

                  </el-col>
                </el-row>
              </el-card>

            </div>
            <div v-else>
              <el-empty description="新增无相关描述"></el-empty>
            </div>
          </el-col>
          <el-col :span="12">
            <el-form-item label="图片素材">
              <image-upload v-model="form.imgUrl" :limit="1"/>
            </el-form-item>
            <el-form-item label="备注说明" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listAppletImg, getAppletImg, delAppletImg, addAppletImg, updateAppletImg} from "@/api/applet/appletImg";

export default {
  name: "AppletImg",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // appletImg表格数据
      appletImgList: [],
      // 弹出层标题
      title: "",
      isUpdate: false,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        imgUrl: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        imgUrl: [
          {required: true, message: "图片地址不能为空", trigger: "blur"}
        ],
        remark: [
          {required: true, message: "备注说明不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询appletImg列表 */
    getList() {
      this.loading = true;
      listAppletImg(this.queryParams).then(response => {
        this.appletImgList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        imgId: null,
        imgUrl: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.imgId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.isUpdate = false;
      this.open = true;
      this.title = "添加图片素材";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.isUpdate = true;
      const imgId = row.imgId || this.ids
      getAppletImg(imgId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "添加图片素材";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.imgId != null) {
            updateAppletImg(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAppletImg(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const imgIds = row.imgId || this.ids;
      this.$modal.confirm('是否确认删除请接口：/without/img/getImg/' + imgIds + ' 的数据项？').then(function () {
        return delAppletImg(imgIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('applet/appletImg/export', {
        ...this.queryParams
      }, `appletImg_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
